#include "node.h"
#include "sic-parser.hh"
#include "horn-common.h"
#include <sstream>
#include <iostream>
#include <stdio.h>

/* Horn Provided Utilities */
extern bool yyis_known_location (const char* loc);
extern int yylocation_line (const char* loc);

int S_Node::line_number ()
{
    return yylocation_line(loc());
}

void S_Node::print (std::ostream& out, int indent)
{
    out << "(" << ast_op ();
    for (int count = 0; count < arity(); count++)
    {
        out << "\n";
        for (int x = 0; x < indent; out << "    ", x++);
        out << " ";
        child(count)->print(out, indent + 1);
    }
    out << ")\n";
}

const char* S_Node::ast_op () {
    int syn = syntax ();
    const char* name;
    if (syn == -1) {
        if (oper () == NULL)
            return "[]";
        else
            syn = oper ()->syntax ();
    }

    name = yyexternal_token_name (syn);
    if (name[0] == '"') {
        char* new_name = strdup (name[1] == '@' ? name+2 : name+1);
        name = new_name;
        new_name[strlen (name)-1] = '\0';
    }

    return name;
}

void S_Node::semantics () {
    //Fill in Semantic Analysis
}

void* S_Node::eval () {
    for (int count = 0; count < arity (); count++)
        child(count)->eval();
}

void S_Token::print (std::ostream& out, int indent)
{
    out << as_string(); 
}
